package cn.zch.test.wordsocr;
import cn.zch.po.jsonpo.plugintooljsonmodelgen.BaiduOcrWords;
import cn.zch.util.constant.ConBaiDuApi;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
 * @author zch
 */
public class WordsOcr {
  /**
   * basicGeneral  outPutLeven 0 全部输出 1 只输出words_result 2 输出需要的words
   */
  public static void basicGeneral(String path1, int outPutLeven) {
    // 初始化一个AipOcr
    AipOcr client = new AipOcr(ConBaiDuApi.WORDS_OCR_APP_ID, ConBaiDuApi.WORDS_OCR_API_KEY, ConBaiDuApi.WORDS_OCR_SECRET_KEY);
    // 传入可选参数调用接口
    HashMap<String, String> options = new HashMap<>();
    options.put("language_type", "CHN_ENG");
    options.put("detect_direction", "true");
    options.put("detect_language", "true");
    options.put("probability", "true");
    // 初始化一个AipOcr
    //AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
    // 可选：设置网络连接参数
    client.setConnectionTimeoutInMillis(2000);
    client.setSocketTimeoutInMillis(60000);
    // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
    //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
    //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
    // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
    // 也可以直接通过jvm启动参数设置此环境变量
    System.setProperty("aip.log4j.conf", "properties/log4j.properties");
    // 调用接口
    //String path1 = "E:\\a.jpg";
    JSONObject res = client.basicGeneral(path1, options);
    if (outPutLeven == 0) {
      System.out.println(res.toString(2));
    }
    else if (outPutLeven == 1) {
      System.out.println(res.toString());
      System.out.println();
      Iterator<String> it = res.keys();
      while (it.hasNext()) {
        // 获得key
        String key = it.next();
        Object value = res.get(key);
        System.out.println("key: " + key + ",value:" + value);
      }
      System.out.println();
      System.out.println(res.get("words_result"));
    }
    else {
      System.out.println();
      Iterator<String> it = res.keys();
      while (it.hasNext()) {
        // 获得key
        String key = it.next();
        Object value = res.get(key);
        System.out.println("key: " + key + ",value:" + value);
      }
      Object jsonObject = JSON.toJSON(res.get("words_result"));
      System.out.println("1:");
      System.out.println(jsonObject);
      String wordsResult = res.get("words_result").toString().replace("[", "").replace("]", "");
      System.out.println("2:");
      System.out.println(wordsResult);
      Object jsonObject3 = JSON.toJSON(wordsResult);
      System.out.println("3:");
      System.out.println(jsonObject3);
      //			com.alibaba.fastjson.JSONObject jsonObject2 = JSON.parseObject(wordsResult);
      //			System.out.println("4:");
      //			System.out.println(jsonObject2);
      JSONArray parseArray = JSON.parseArray(res.get("words_result").toString());
      System.out.println("5:");
      System.out.println(parseArray);
      /*-----------------------------------*/
      System.out.println("6:");
      Iterator<Object> ita = parseArray.iterator();
      List<BaiduOcrWords> list = new ArrayList<BaiduOcrWords>();
      while (ita.hasNext()) {
        //				Object ob = ita.next();
        //				System.out.println(ob);
        com.alibaba.fastjson.JSONObject ob2 = (com.alibaba.fastjson.JSONObject) JSON.toJSON(ita.next());
        assert ob2 != null;
        System.out.println(ob2.getString("words"));
      }
      /*-----------------------------------*/
    }
  }
  public static void main(String[] args) {
    basicGeneral("E:\\a.jpg", 2);
  }
}
